package cn.microservices.cloud.system.service.impl;

import cn.microservices.cloud.system.api.bo.EmployeeBO;
import cn.microservices.cloud.system.api.bo.UserInfoBO;
import cn.microservices.cloud.system.api.dto.LoginDTO;
import cn.microservices.cloud.system.api.dto.RegisterDTO;
import cn.microservices.cloud.system.api.service.AuthService;
import cn.microservices.cloud.system.dao.dataobject.EmployeeDO;
import cn.microservices.cloud.system.dao.mapper.EmployeeMapper;
import cn.microservices.cloud.system.service.convert.AuthConvert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 返回BO
     * @param registerDTO
     * @return
     */
    @Override
    public EmployeeBO register(RegisterDTO registerDTO) {
        EmployeeDO employeeDO = AuthConvert.INSTANCE.convert(registerDTO);
        employeeMapper.insert(employeeDO);
        return AuthConvert.INSTANCE.convert(employeeDO);
    }

    @Override
    public UserInfoBO findUserInfo(LoginDTO loginDTO) {
        if (loginDTO.getIdentityType().equals("EMP")){
            LambdaQueryWrapper<EmployeeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(EmployeeDO::getCellphone,loginDTO.getUsername());
            EmployeeDO employeeDO = employeeMapper.selectOne(lambdaQueryWrapper);
            return AuthConvert.INSTANCE.convertUserInfoBO(employeeDO);
        }
        return null;
    }


}